package com.example.himalaya.data;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.himalaya.base.BaseApplication;
import com.example.himalaya.utils.Constants;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.Announcer;

import java.util.ArrayList;
import java.util.List;

public class SubDao implements ISubDao {

    private static final String TAG = "SubDao";

    private static final SubDao sSubDao = new SubDao();
    private final XimalayaDBHelper mXimalayaDBHelper;
    private ISubCallback mSubCallback;
    private Object mLock = new Object();


    public static SubDao getSubDao() {
        return sSubDao;
    }

    private SubDao() {
        mXimalayaDBHelper = new XimalayaDBHelper(BaseApplication.getAppContext());
    }

    @Override
    public void setCallback(ISubCallback subCallback) {
        this.mSubCallback = subCallback;
    }

    @Override
    public void addSub(Album album) {
        synchronized (mLock) {

            SQLiteDatabase db = null;
            boolean isSuccess = false;
            try {
                db = mXimalayaDBHelper.getWritableDatabase();
                db.beginTransaction();//开启事务

                ContentValues values = new ContentValues();
                values.put(Constants.SUB_ALBUM_ID, album.getId());
                values.put(Constants.SUB_TITLE, album.getAlbumTitle());
                values.put(Constants.SUB_COVER_URL, album.getCoverUrlLarge());
                values.put(Constants.SUB_DESCRIPTION, album.getAlbumIntro());
                values.put(Constants.SUB_TRACKS_COUNT, album.getIncludeTrackCount());
                values.put(Constants.SUB_PLAY_COUNT, album.getPlayCount());
                values.put(Constants.SUB_AUTHOR_NAME, album.getAnnouncer().getNickname());

                db.insert(Constants.SUB_TB_NAME, null, values);
                isSuccess = true;
                db.setTransactionSuccessful();
            } catch (Exception e) {
                isSuccess = false;
                e.printStackTrace();
            }finally {
                if (db != null) {
                    db.endTransaction();
                    db.close();
                }
                if (mSubCallback != null) {
                    mSubCallback.onAddSub(isSuccess);
                }
            }
        }

    }

    @Override
    public void delSub(Album album) {
        synchronized (mLock) {

            SQLiteDatabase db = null;
            boolean isDeleteSuccess = false;
            try {
                db = mXimalayaDBHelper.getWritableDatabase();
                db.beginTransaction();//开启事务

                int deleteLineNum = db.delete(Constants.SUB_TB_NAME, Constants.SUB_ALBUM_ID + "=?", new String[]{album.getId() + ""});
                Log.d(TAG, "delSub: deleteLineNum-->"+deleteLineNum);
                isDeleteSuccess = true;
                db.setTransactionSuccessful();
            } catch (Exception e) {
                isDeleteSuccess = false;
                e.printStackTrace();
            }finally {
                if (db != null) {
                    db.endTransaction();
                    db.close();
                }

                if (mSubCallback != null) {
                    mSubCallback.onDelSub(isDeleteSuccess);
                }
            }
        }
    }

    @Override
    public void getSubList() {
        synchronized (mLock) {
            SQLiteDatabase db = null;
            List<Album> subAlbums = new ArrayList<>();
            try {
                db = mXimalayaDBHelper.getReadableDatabase();
                db.beginTransaction();//开启事务

                Cursor cursor = db.query(Constants.SUB_TB_NAME, null, null, null, null, null, "_id desc");
                while (cursor.moveToNext()) {
                    Album album = new Album();

                    String coverUrl = cursor.getString(cursor.getColumnIndex(Constants.SUB_COVER_URL));
                    album.setCoverUrlLarge(coverUrl);
                    //
                    String title = cursor.getString(cursor.getColumnIndex(Constants.SUB_TITLE));
                    album.setAlbumTitle(title);
                    //
                    String description = cursor.getString(cursor.getColumnIndex(Constants.SUB_DESCRIPTION));
                    album.setAlbumIntro(description);
                    //
                    int tracksCount = cursor.getInt(cursor.getColumnIndex(Constants.SUB_TRACKS_COUNT));
                    album.setIncludeTrackCount(tracksCount);
                    //
                    int playCount = cursor.getInt(cursor.getColumnIndex(Constants.SUB_PLAY_COUNT));
                    album.setPlayCount(playCount);
                    //
                    int albumId = cursor.getInt(cursor.getColumnIndex(Constants.SUB_ALBUM_ID));
                    album.setId(albumId);
                    String authorName = cursor.getString(cursor.getColumnIndex(Constants.SUB_AUTHOR_NAME));
                    Announcer announcer = new Announcer();
                    announcer.setNickname(authorName);
                    album.setAnnouncer(announcer);
                    subAlbums.add(album);
                }

                db.setTransactionSuccessful();
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if (db != null) {
                    db.endTransaction();
                    db.close();
                }

                if (mSubCallback != null) {
                    mSubCallback.onGetSubList(subAlbums);
                }
            }
        }
    }
}
